package com.rainng.coursesystem.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.rainng.coursesystem.controller.BaseController;
import com.rainng.coursesystem.dao.mapper.GeneralReportMapper;
import com.rainng.coursesystem.model.entity.GeneralReportEntity;
import com.rainng.coursesystem.model.excelvo.GeneralReportExcelVo;
import com.rainng.coursesystem.model.excelvo.TaskExcelVo;
import com.rainng.coursesystem.model.shitong.*;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.service.CheckFileService;
import com.rainng.coursesystem.service.GeneralReportService;
import com.rainng.coursesystem.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


@Api(tags = "总报告接口")
@RequestMapping("/v2/general/report")
@RestController
public class GeneralReportController extends BaseController {

    @Resource
    private GeneralReportService service;

    @Resource
    private GeneralReportMapper generalReportMapper;

    //增改接口
    @PostMapping("/task/save")
    @ApiOperation(value = "任务管理:总报告新增接口(根据申请编号新增或修改)")
    public ResultVO taskReportSave(@RequestBody GeneralReportSaveVo cvo) {
        return service.saveGeneralReportFromTask(cvo);
    }

    //查看详情接口
    @PostMapping("/task/delete/{id}")
    @ApiOperation(value = "任务管理:根据申请编号删除")
    public ResultVO taskReportDelete(@PathVariable String id) {
        return service.deleteGeneralReportByApplyNum(id);
    }

    @GetMapping("/task/detail/{id}")
    @ApiOperation(value = "任务管理:根据申请编号查看详情")
    public ResultVO getTaskDetailItems(@PathVariable String id) {
        ResultVO res = service.getGeneralReportDetailsByApplyNum(id);
        return res;
    }

//    @PostMapping("/save")
//    @ApiOperation(value = "新建总报告")
//    public ResultVO listSave(@RequestBody GeneralReportVo cvo) {
//        return service.saveGeneralReport(cvo);
//    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据主键删除")
    public ResultVO listDelete(@PathVariable String id) {
        return service.deleteGeneralReportById(id);
    }

    @PostMapping("/page/count")
    @ApiOperation(value = "返回分页查询数量")
    public ResultVO listCount(@RequestBody GeneralReportVo cvo) {
        return service.countGeneralReport(cvo);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultVO listPage(@RequestBody GeneralReportVo cvo) {
        return service.selectGeneralReport(cvo);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据applyNum查看总报告详情")
    public ResultVO getGeneralReportByApplyNum(@PathVariable String id) {
        ResultVO res = service.getGeneralReportByApplyNum(id);
        return res;
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "detail查看详情")
    public ResultVO getDetailItems(@PathVariable String id) {
        ResultVO res = service.getGeneralReportDetailsByFatherId(id);
        return res;
    }

    @PostMapping("/update")
    @ApiOperation(value = "总报告更新")
    public ResultVO updateGeneralReport(@RequestBody GeneralReportUpdatePo po) {
        ResultVO res = service.updateGeneralReportById(po);
        return res;
    }

    @PostMapping("/detail/update")
    @ApiOperation(value = "detail更新")
    public ResultVO updateDetailItems(@RequestBody GeneralReportDetailUpdatePo po) {
        ResultVO res = service.updateGeneralReportDetailById(po);
        return res;
    }

    @PostMapping("/detail/multi/update")
    @ApiOperation(value = "总报告detail批量更新接口")
    public ResultVO updateMultiDetailItems(@RequestBody List<GeneralReportDetailUpdatePo> pos) {
        ResultVO res = service.updateMultiGeneralReportDetailById(pos);
        return res;
    }

    @PostMapping("/excel/upload/{id}")
    @ApiOperation(value = "驻场需求,总报告任务导入上传接口,传applyNum")
    public ResultVO importExcelSubject(MultipartFile file,@PathVariable String id) throws IOException {

        List<GeneralReportExcelVo> list = EasyExcelUtil.readFromZero(file.getInputStream(),
                GeneralReportExcelVo.class);
        if (list.size() > 100) {
            return super.failedResult("单次导入不能超过100条");
        }
        //根据applynum查找task参数
        List<Map<String, Object>> taskinfo = generalReportMapper.selectTaskInfoByApplyNum(id);
        if (taskinfo.size() == 0) {
            return super.failedResult("任务申请编号不存在");
        }
        Map<String, Object> m = taskinfo.get(0);
        service.saveGeneralReportByImport(list,id,m);
//        CompletableFuture daoTask = CompletableFuture.runAsync(()->{
//            //System.out.println("开始");
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            //System.out.println("等待5秒");
//            service.saveGeneralReportByImport(list,id,m);
//            //System.out.println("finish");
//        });

        return super.result("导入成功,请稍后查看");
    }


}
